#line 1 "source/LIS302_test_CCS.c"
#line 1 "source/LIS302_test_CCS.c"

#line 9 "source/LIS302_test_CCS.c"
 




#line 1 "source/lis302_i2c.h"

#line 7 "source/lis302_i2c.h"
 
                           
#use I2C(master, sda=PIN_B0, scl=PIN_B1)
 
#line 12 "source/lis302_i2c.h"
#line 13 "source/lis302_i2c.h"
                           
#line 15 "source/lis302_i2c.h"
#line 16 "source/lis302_i2c.h"
#line 17 "source/lis302_i2c.h"

#line 19 "source/lis302_i2c.h"

#line 21 "source/lis302_i2c.h"
#line 22 "source/lis302_i2c.h"
#line 23 "source/lis302_i2c.h"

#line 25 "source/lis302_i2c.h"

void NOP()
{
 #asm 
 NOP 
 #endasm
 }                                       

                                                           
int8 lis302_xfer(int8 AdSpi, int8 DataOut)
{                              
   int8 i;
   int8 DataIn=0;     
   
   
#line 72 "source/lis302_i2c.h"
                          
   return DataIn;                           
}

boolean i2c_rdy(unsigned int8 address)
{
   int1 ack;
   i2c_start();            
   ack = i2c_write(address);  
   i2c_stop();
   return !ack;
}


void lis302_write_byte(BYTE address, BYTE data) 
{

   i2c_start();
   i2c_write((0x3A |(BYTE)(address>>7))&0xfe);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
}

unsigned int8 lis302_read_byte(BYTE address) 
{
   unsigned int8 data;

   i2c_start();
   i2c_write((0x3A |(BYTE)(address>>7))&0xfe);
   i2c_write(address);
   i2c_start();
   i2c_write((0x3A |(BYTE)(address>>7))|1);
   data=i2c_read(0);
   i2c_stop();
   return(data);
}


void init_LIS302(void)
{
   int tmp;     

   
   lis302_write_byte(0x20 ,0x47 );  
       
   delay_ms(100);
              
   tmp=lis302_read_byte(0x8f );

   LED1=LED_ON;
  
#line 131 "source/lis302_i2c.h"
   
   delay_ms(1000);                               
}                        

#line 14 "source/LIS302_test_CCS.c"

#line 1 "source/typedef.h"
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
typedef signed short s8;
typedef signed int s16;
typedef signed long s32;
#line 15 "source/LIS302_test_CCS.c"


typedef unsigned int8 u8;
typedef unsigned int16 u16;
typedef signed int8 s8;
typedef signed int16 s16;

#line 23 "source/LIS302_test_CCS.c"

void main()
{
   
   int8 xval,yval,zval;

   
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   lcd_init();
   
   
   TRIS_LED1=0;
   TRIS_LED2=0;
   
   
   TRISC=0;
   TRISE=0;
   
   SET_TRIS_C(0);
   SET_TRIS_E(0);
   
   LED1=LED_OFF;
   
   lcd_gotoxy(1,1);
   
   
   printf(lcd_putc,"    PICSTAR    ");
   lcd_gotoxy(1,2);
   
   printf(lcd_putc,"   Z-CONTROL    ");
   delay_ms(1000);
   
   lcd_gotoxy(1,2);
   printf(lcd_putc," ACCELEROMETRE   ");
   
   LCD_BACKLIGHT(LCD_ON);
  
   init_LIS302();
   
   delay_ms(1000);
     
   lcd_gotoxy(1,2);
   
   if(i2c_rdy(0x3A )) printf(lcd_putc,"  found LIS302  ");
   else {printf(lcd_putc,"   no LIS302     ");
         while(1);
        }
   delay_ms(1000);     

   
   lcd_gotoxy(1,1);
   printf(lcd_putc," X     Y     Z    ");

   
   while(1)
      {  
  
   LED1=LED_ON;
   while(!bit_test(lis302_read_byte(0xA7 ),3 ));    
   LED1=LED_OFF;


   LED2=LED_ON;
   xval = lis302_read_byte(0xA9 );                     
   yval = lis302_read_byte(0xAB );
   zval = lis302_read_byte(0xAD ); 
   LED2=LED_OFF;
    
   lcd_gotoxy(1,2); 
   printf(lcd_putc,"%03u   %03u   %03u    ",xval,yval,zval);
   
 
   delay_ms(200);
   
      }
}
